﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess
{
    /// <summary>
    /// 选手
    /// </summary>
    public abstract class Player
    {
        protected string _name;
        protected string _tag;
        private bool _isFirstPalyer;  //true为选手1，false为选手2
        protected bool _isAutoWantPlay = false;  //是否自动下棋

        protected List<ChessPiece> _chessPieces;  //选手的棋子

        public ChessPiece GetChessPiece(int row,int col) {
            return _chessPieces?.FirstOrDefault(x => x.IsMine(row, col));
        }

        public List<ChessPiece> GetChessPieces() {
            return _chessPieces;
        }

        public Boolean IsMyChessPiece(int row,int col) {
            var cp = GetChessPiece(row,col);
            return cp != null ? true : false;
        }

        public Boolean IsMyChessPiece((int row, int col) pos)
        {
            var cp = GetChessPiece(pos.row, pos.col);
            return cp != null ? true : false;
        }

        public bool IsWantPlay
        {
            get { return _isAutoWantPlay; }
        }

        /// <summary>
        /// 选手名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// 棋子标记
        /// </summary>
        public string Tag
        {
            get { return _tag; }          
        }

        /// <summary>
        /// 初始化自己的棋子
        /// </summary>
        public void InitChessPieces(string tag,Boolean isFirstPalyer) {
            this._tag = tag;
            this._isFirstPalyer = isFirstPalyer;
            _chessPieces = new List<ChessPiece>();

            if (isFirstPalyer)
            {
                _chessPieces.Add(new ChessPiece(3, 0, tag));
                _chessPieces.Add(new ChessPiece(3, 1, tag));
                _chessPieces.Add(new ChessPiece(3, 2, tag));
                _chessPieces.Add(new ChessPiece(3, 3, tag));
            }
            else {
                _chessPieces.Add(new ChessPiece(0, 0, tag));
                _chessPieces.Add(new ChessPiece(0, 1, tag));
                _chessPieces.Add(new ChessPiece(0, 2, tag));
                _chessPieces.Add(new ChessPiece(0, 3, tag));
            }
        }

        /// <summary>
        /// 重新初始化自己的棋子
        /// </summary>
        public void ReInitChessPieces() {
            if (_isFirstPalyer)
            {
                _chessPieces[0].Move(3, 0);
                _chessPieces[1].Move(3, 1);
                _chessPieces[2].Move(3, 2);
                _chessPieces[3].Move(3, 3);
            }
            else {
                _chessPieces[0].Move(0, 0);
                _chessPieces[1].Move(0, 1);
                _chessPieces[2].Move(0, 2);
                _chessPieces[3].Move(0, 3);
            }
        }

        /// <summary>
        /// 建议下棋走位
        /// </summary>
        public virtual (ChessPiece cp, Dir dir)? IWantPlay(ChessBoard chessBoard) {

            return null;
        }


        /// <summary>
        /// 是否输掉
        /// </summary>
        public Boolean IsLost
        {
            get { return !_chessPieces.Exists(x => !x.IsDead); }
        }
    }
}
